package org.alex.samples.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alex.samples.model.User;
import org.springframework.stereotype.Repository;

@Repository
public class UserDAOImpl implements IUserDAO {

	private static Map<Integer,User> users=new HashMap<Integer,User>();
	
	public UserDAOImpl(){
		users.put(1, new User(1,"alex",20));
		users.put(2, new User(2,"join",21));
		users.put(3, new User(3,"james",22));
		users.put(4, new User(4,"kobe",15));
		users.put(5, new User(5,"antonni",26));
		users.put(6, new User(6,"wjn",19));
		users.put(7, new User(7,"cjq",18));
	}
	
	@Override
	public List<User> findAllUsers() {
		if(users != null){
			return new ArrayList<User>(users.values());
		}else{
			return null;
		}
	}

	@Override
	public User getUserById(int id) {
		if(users.containsKey(id)){
			return users.get(id);
		}else{
			return null;
		}
	}

	@Override
	public void editUser(User user) throws Exception {
		if(user != null && users.containsKey(user.getId())){
			users.put(user.getId(), user);
		}else{
			throw new Exception("Update User Fail!");
		}
		
	}

	@Override
	public void createUser(User user) throws Exception {
		if(user !=null && !users.containsKey(user.getId())){
			users.put(user.getId(), user);
		}else{
			throw new Exception("create User Fail!");
		}
		
	}

	@Override
	public void deleteUser(int id) throws Exception {
		if( users.containsKey(id)){
			users.remove(id);
		}else{
			throw new Exception("Delete User Fail!");
		}
		
	}
	
	
}
